/*
 * A simple ToDo Manager
 *
 * Copyright (c) 2011 Nosheen Zaza, Manuel Rebollo, Seyedsaeid Mahpeikar and
 * Wo Ke
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package todomanager.ui;

import java.awt.FlowLayout;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;

/**
 * This is the clock for this application. IT displays the current time.
 * 
 * @author
 * Manuel Rebollo
 */
public class Clock extends JPanel {

    private JLabel time;

    public Clock() {
        time = new JLabel();
        time.setFont(UIManager.getDefaults().getFont("Font.Dialog-Bold-10"));
        time.setBorder((BorderFactory.createEmptyBorder(0, 30, 10, 0)));

        setLayout(new FlowLayout(FlowLayout.RIGHT));
        add(time);
        setOpaque(false);
    }

    public void run() {
        ThreadClock f = new ThreadClock(this);
        f.start();
        setVisible(true);
    }

    public JLabel getjLabel1() {
        return time;
    }

    /**
     *
     * @author Manuel Rebollo
     */
    private class ThreadClock extends Thread {

        Clock clock;
        private ResourceBundle settings;

        public ThreadClock(Clock window) {
            this.clock = window;
            settings = ActionControl.getInstance().getStringsBundle();
        }

        public void run() {
            GregorianCalendar f;
            while (true) {
                try {

                    Thread.sleep(1000);

                } catch (InterruptedException ex) {
                    Logger.getLogger(ThreadClock.class.getName()).log(Level.SEVERE, null, ex);
                }

                f = new GregorianCalendar();
                clock.getjLabel1().setText(settings.getString("CurentTime") + ": " + f.get(Calendar.HOUR_OF_DAY) + ":" + f.get(Calendar.MINUTE) + ":" + f.get(Calendar.SECOND) + "");
            }
        }
    }
}
